import {
  get,
  post
} from '../../../../request/index'
   
let timer;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "",
    codeMore: "",
    checked: false,
    imgIcon: "https://ryansystem-bucket.oss-cn-shenzhen.aliyuncs.com/upload/choice_icon.png",
    imgIcon1: "",
    isShow: false,
    moShow: false,
    show: true,
    show1: false,
    show2: false,
   
    loading: true,
    workTypeList: [],
    content: "",
    showOneButtonDialog: false,
    img: "",
    name: "",
    meetingUrl: [], //班前要开会
    prepareUrl: [], //准备要充分
    safecheckUrl: [], //安全要确定
    workcheckUrl: [], //工作要检查
    exchangeUrl: [], //交班要清楚
    sort: ";",
    sortNumber: "",
    dataTime: "", //整改时间
    chanName: "", //责任人
    srt: "",
    dataText: "", //整改措施
    workStatus: "", //整改结果
    jsonType: [{
        label: "已整改",
        value: 1
      },
      {
        label: "未整改",
        value: 2
      }
    ],
    net: {
      url: '/app/memberinfo/getList',
      method: 'post',
      key: 'memberId',
      data:{
        mineId: true,
      },
      // value: 'memberId'
      value:'nickName'
    },
    meetinga: "",
    meetingb: "",
    meetingc: "",
    preparea: "",
    prepareb: "",
    preparec: "",
    safechecka: "",
    safecheckb: "",
    safecheckc: "",
    workchecka: "",
    workcheckb: "",
    workcheckc: "",
    workcheckd: "",
    workchecke: "",
    exchangea: "",
    exchangeb: "",
    exchangec: "",
    error: '',
    seachList:[],
    inputName:"",
    showMaster:false,
    baseButton:[{text:'确定'}],
    prolist:wx.getStorageSync('prolist')==''?wx.getStorageSync('prolist'):JSON.parse(wx.getStorageSync('prolist')),
    numberSex:"",
    score:'#ffffff'

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {
    const {
      userid,
      status
    } = this.data.prolist
    console.log(userid,
      status);
    if (userid == undefined && status == undefined) {
      this.setData({
        codeMore: "1"
      })
    } else {
      if (userid == '' && status == "0") {
        
        this.setData({
          codeMore: "1"
        })
      } else {
        this.setData({
          codeMore: "2"
        })
      }
    }



    this.Initial()
    this.getlogin()


  },
  getlogin() {
    let that = this
    wx.login({
      success(res) {
        if (res.code) {

          that.setData({
            code: res.code
          })

        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  onClose() {

    this.setData({
      show: false,
      loading: false,
      show1: false,
      show2: false
    });

  },
  getPhoneNumber(e) {
    console.log(e);
    let that = this;
    const {
      errMsg,
      iv,
      encryptedData
    } = e.detail


    if (errMsg == "getPhoneNumber:fail user deny") {
      return
    }

    wx.checkSession({
      success(res) {

        if (that.data.code) {

          get('/wx/wx/getUserInfoByCode', {
            code: that.data.code,
            encryptedData: encryptedData,
            iv: iv,
          }).then(res => {
            console.log(res);
            if (res.data == null) {
              that.getlogin()
              wx.showToast({
                title: '请到平台激活再使用',
                duration: 5000,
                icon: 'none',
                mask: true
              })
            } else {
              if (res.data.userid === "") {
                wx.showToast({
                  title: '您还未注册请先注册',
                  duration: 3000,
                  mask: true
                })
                wx.navigateTo({
                  url: `/pages/register/register?openid=${res.data.openid}`
                })
                return
              }


              if (!res.data.status) {
                wx.showToast({
                  title: '请重新登录',
                  duration: 3000,
                  mask: true
                })

                return
              } else {
                wx.setStorageSync('prolist', JSON.stringify(res.data))
                that.setData({
                  show: false,
                  loading: false,
                  show1: false,
                  show2: false
                });
              }



            }



          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },

    })

  },
  onChange(event) {
    this.setData({
      result: event.detail,
    });
  },
 
  checkChange(event) {
    this.setData({
      checked: event.detail,
    });
    if (this.data.checked) {
      this.pageScrollToBottom()
      let list = this.data.workTypeList
      list.forEach((item, index) => {
        item.childrens = item.childrens.map((i, idx) => {
          return {
            name: i.name,
            regulation: i.regulation,
            sort: i.sort,
            meatlist: [{
              type: 1,
              typeText: '已落实',
              sort: i.sort,
              sureid: true,
              check: i.check,
              index: index,
              inx: idx,
            }, ]
          }


        })

      });
      this.setData({
        workTypeList: list,
        meetinga: "1",
        meetingb: "1",
        meetingc: "1",
        preparea: "1",
        prepareb: "1",
        preparec: "1",
        safechecka: "1",
        safecheckb: "1",
        safecheckc: "1",
        workchecka: "1",
        workcheckb: "1",
        workcheckc: "1",
        workcheckd: "1",
        workchecke: "1",
        exchangea: "1",
        exchangeb: "1",
        exchangec: "1",
      })

    } else {
      let list = this.data.workTypeList
      list.forEach((item, index) => {
        item.childrens = item.childrens.map((i, idx) => {
          return {
            name: i.name,
            regulation: i.regulation,
            sort: i.sort,
            meatlist: [{
              type: 1,
              typeText: '已落实',
              sort: i.sort,
              sureid: false,
              check: i.check,
              index: index,
              inx: idx,
            }, ]
          }


        })

      });
      this.setData({
        workTypeList: list,
        meetinga: "",
        meetingb: "",
        meetingc: "",
        preparea: "",
        prepareb: "",
        preparec: "",
        safechecka: "",
        safecheckb: "",
        safecheckc: "",
        workchecka: "",
        workcheckb: "",
        workcheckc: "",
        workcheckd: "",
        workchecke: "",
        exchangea: "",
        exchangeb: "",
        exchangec: "",
      })

    }


  },

  Initial() {
    post('/app/lineDirectornet/comeIn').then(res=>{
      console.log(res);
      this.setData({
        numberSex:res.data
      })

    })
    post('/app/lineDirectornet/getList?currentPage=1&pageSize=100', {
      workType: 2
    }).then(res => {
      console.log(res);

      let list = res.data.list

      list.forEach((item, index) => {

        item.childrens = item.childrens.map((i, inx) => {
            return {

              name: i.name,
              regulation: i.regulation,
              sort: i.sort,

              meatlist: [{
                  type: 1,
                  typeText: '已落实',
                  sort: i.sort,
                  sureid: false,
                  check: i.check,
                  index: index,
                  inx: inx,
                }

              ]
            }

          }

        )

      })
      console.log(list);
      this.setData({
        workTypeList: res.data.list
      })
    })

  },
  tapOneDialogButton(e) {
    console.log(e);
    this.setData({
      showOneButtonDialog: true,
      content: e.currentTarget.dataset.item
    })
  },

  tapDialogButton(e) {

    this.setData({
      showOneButtonDialog: false
    })
  },

  onClose() {

    this.setData({
      show: false,
      loading: false,
      show1: false,
      show2: false
    });

  },
  tapOne(e) {
    console.log(e);
    let index = e.currentTarget.dataset.selectleft;
    let inx = e.currentTarget.dataset.inx
    let idx = e.currentTarget.dataset.index
    let sort = e.currentTarget.dataset.sort;
    let workTypeList = this.data.workTypeList
    let check = e.currentTarget.dataset.check
    workTypeList[idx].childrens[inx].meatlist[index].sureid = !workTypeList[idx].childrens[inx].meatlist[index].sureid
    this.setData({
      workTypeList: workTypeList,
    })

    if (sort == 1 && check == false) {
      this.setData({
        meetinga: "1"
      })
    } else if (sort == 2 && check == false) {
      this.setData({
        meetingb: "1"
      })
    } else if (sort == 3 && check == false) {
      this.setData({
        meetingc: "1"
      })
    } else if (sort == 4 && check == false) {
      this.setData({
        preparea: "1"
      })
    } else if (sort == 5 && check == false) {
      this.setData({
        prepareb: "1"
      })
    } else if (sort == 6 && check == false) {
      this.setData({
        preparec: "1"
      })
    } else if (sort == 7 && check == false) {
      this.setData({
        safechecka: "1"
      })
    } else if (sort == 8 && check == false) {
      this.setData({
        safecheckb: "1"
      })
    } else if (sort == 9 && check == false) {
      this.setData({
        safecheckc: "1"
      })
    } else if (sort == 10 && check == false) {
      this.setData({
        workchecka: "1"
      })
    } else if (sort == 11 && check == false) {
      this.setData({
        workcheckb: "1"
      })
    } else if (sort == 12 && check == false) {
      this.setData({
        workcheckc: "1"
      })
    } else if (sort == 13 && check == false) {
      this.setData({
        workcheckd: "1"
      })
    } else if (sort == 14 && check == false) {
      this.setData({
        workchecke: "1"
      })
    } else if (sort == 15 && check == false) {
      this.setData({
        exchangea: "1"
      })
    } else if (sort == 16 && check == false) {
      this.setData({
        exchangeb: "1"
      })
    } else if (sort == 17 && check == false) {
      this.setData({
        exchangec: "1"
      })
    }



    if (sort == 1 && check == true) {
      this.setData({
        meetinga: ""
      })
    } else if (sort == 2 && check == true) {
      this.setData({
        meetingb: ""
      })
    } else if (sort == 3 && check == true) {
      this.setData({
        meetingc: ""
      })
    } else if (sort == 4 && check == true) {
      this.setData({
        preparea: ""
      })
    } else if (sort == 5 && check == true) {
      this.setData({
        prepareb: ""
      })
    } else if (sort == 6 && check == true) {
      this.setData({
        preparec: ""
      })
    } else if (sort == 7 && check == true) {
      this.setData({
        safechecka: ""
      })
    } else if (sort == 8 && check == true) {
      this.setData({
        safecheckb: ""
      })
    } else if (sort == 9 && check == true) {
      this.setData({
        safecheckc: ""
      })
    } else if (sort == 10 && check == true) {
      this.setData({
        workchecka: ""
      })
    } else if (sort == 11 && check == true) {
      this.setData({
        workcheckb: ""
      })
    } else if (sort == 12 && check == true) {
      this.setData({
        workcheckc: ""
      })
    } else if (sort == 13 && check == true) {
      this.setData({
        workcheckd: ""
      })
    } else if (sort == 14 && check == true) {
      this.setData({
        workchecke: ""
      })
    } else if (sort == 15 && check == true) {
      this.setData({
        exchangea: ""
      })
    } else if (sort == 16 && check == true) {
      this.setData({
        exchangeb: ""
      })
    } else if (sort == 17 && check == true) {
      this.setData({
        exchangec: ""
      })
    }


  },

  tapTwo(e) {

    let index = e.currentTarget.dataset.selectleft;
    let inx = e.currentTarget.dataset.inx
    let idx = e.currentTarget.dataset.index
    let sort = e.currentTarget.dataset.sort;
    let workTypeList = this.data.workTypeList
    workTypeList[idx].childrens[inx].meatlist[index].sureid = false




    this.setData({
      workTypeList: workTypeList,
      show2: true,
      sortNumber: sort,
      dataTime: '', //整改时间
      chanName: '', //责任人
      dataText: '', //整改措施
      workStatus: ''

    })

  },
  changea(e) {
    console.log(e);
    this.setData({
      chanName: e.detail
    })
  },
  changeb(e) {
    console.log(e);
    this.setData({
      dataTime: e.detail
    })
  },
  changec(e) {
    console.log(e);
    this.setData({
      dataText: e.detail
    })
  },
  changed(e) {
    console.log(e);
    this.setData({
      workStatus: e.detail
    })
  },
  submint(){
   
    let {
      meetinga,
      meetingb,
      meetingc,
      preparea,
      prepareb,
      preparec,
      safechecka,
      safecheckb,
      safecheckc,
      workchecka,
      workcheckb,
      workcheckc,
      workcheckd,
      workchecke,
      exchangea,
      exchangeb,
      exchangec,
    } = this.data


    if (meetinga != "" ||
      meetingb != "" ||
      meetingc != "" ||
      preparea != "" ||
      prepareb != "" ||
      preparec != "" ||
      safechecka != "" ||
      safecheckb != "" ||
      safecheckc != "" ||
      workchecka != "" ||
      workcheckb != "" ||
      workcheckc != "" ||
      workcheckd != "" ||
      workchecke != "" ||
      exchangea != "" ||
      exchangeb != "" ||
      exchangec != ""
    ){
      this.changeSub()
    }else{
      this.setData({
        error: '您还有未填项！'  
      })
      return false
    }
  },

  onChangeout(e) {

    clearTimeout(timer);
    timer = setTimeout(async() => {
      let data = await post(`/app/mineinfo/getList`, {mineName: e.detail}) 
    
        this.setData({
         viewShow:true,
         seachList:data.data.list
        })
    }, 500);

       
  },



  close(){
    this.setData({
      show2:false,
      show1: false,
    })
  },
  tapButton(){
        this.setData({
          showMaster:false
        })
      },

    // 获取滚动条当前位置
  onPageScroll: function (e) {
    console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  //回到顶部
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取容器高度，使页面滚动到容器底部
  pageScrollToBottom: function () {
    wx.createSelectorQuery().select('#j_page').boundingClientRect(function (rect) {
      // 使页面滚动到底部
      wx.pageScrollTo({
        scrollTop: rect.bottom
      })
    }).exec()
  },
  async changeSub(){
    let prolist = wx.getStorageSync('prolist') == '' ? wx.getStorageSync('prolist') : JSON.parse(wx.getStorageSync('prolist'));
 
       let {
       meetinga,
       meetingb,
       meetingc,
       preparea,
       prepareb,
       preparec,
       safechecka,
       safecheckb,
       safecheckc,
       workchecka,
       workcheckb,
       workcheckc,
       workcheckd,
       workchecke,
       exchangea,
       exchangeb,
       exchangec,
       meetingUrl, //班前要开会
       prepareUrl, //准备要充分
       safecheckUrl, //安全要确定
       workcheckUrl, //工作要检查
       exchangeUrl,
        } = this.data 

       let a = meetingUrl.toString()
       let b = prepareUrl.toString()
       let c = safecheckUrl.toString()
       let d = workcheckUrl.toString()
       let e = exchangeUrl.toString()
    let res = await post(`/app/lineSafee/insert`,{
       meetinga:meetinga,
       meetingb:meetingb,
       meetingc:meetingc,
       preparea:preparea,
       prepareb:prepareb,
       preparec:preparec,
       safechecka:safechecka,
       safecheckb:safecheckb,
       safecheckc:safecheckc,
       workchecka:workchecka,
       workcheckb:workcheckb,
       workcheckc:workcheckc,
       workcheckd:workcheckd,
       workchecke:workchecke,
       exchangea:exchangea,
       exchangeb:exchangeb,
       exchangec:exchangec,
       meetingUrl:a, //班前要开会
       prepareUrl:b, //准备要充分
       safecheckUrl:c, //安全要确定
       workcheckUrl:d, //工作要检查
       exchangeUrl:e,
       mineId:prolist.mineId,
       createUser:prolist.userid
    })
    if(res.msg=="success"){
      this.setData({
        showMaster: true,
        checked:false
      })
      this.goTop()
      this.onLoad()
    }
  },
  // 未落实保存数据
  sbuit() {
    let {
      sortNumber,
      dataTime, //整改时间
      chanName, //责任人
      dataText, //整改措施
      workStatus, //整改结果
      sort //排序
    } = this.data
    if (chanName == '' || dataTime == '' || dataText == '') {

      this.setData({
        error: '请先填写再保存'
      })
      return false

    }
    switch (sortNumber) {
      case 1:
        this.setData({
          meetinga: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 2:
        this.setData({
          meetingb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 3:
        this.setData({
          meetingc: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 4:
        this.setData({
          preparea: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 5:
        this.setData({
          prepareb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 6:
        this.setData({
          preparec: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 7:
        this.setData({
          safechecka: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 8:
        this.setData({
          safecheckb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 9:
        this.setData({
          safecheckc: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 10:
        this.setData({
          workchecka: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 11:
        this.setData({
          workcheckb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 12:
        this.setData({
          workcheckc: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 13:
        this.setData({
          workcheckd: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 14:
        this.setData({
          workchecke: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 15:
        this.setData({
          exchangea: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 16:
        this.setData({
          exchangeb: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;
      case 17:
        this.setData({
          exchangec: `2;${chanName};${sort}${dataTime};${dataText};${workStatus}`,
          show2: false
        })
        break;

      default:
        break;
    }
  },

  upint(e) {

    console.log(e);

    this.setData({
      show1: true,
      name: e.currentTarget.dataset.name
    });
  },
  change(e) {
    let name = this.data.name
    switch (name) {
      case '班前要开会':
        let meetingUrl = this.data.meetingUrl

        meetingUrl.push(e.detail)

        this.setData({
          meetingUrl: meetingUrl
        })

        break;
      case '准备要充分':
        let prepareUrl = this.data.prepareUrl

        prepareUrl.push(e.detail)

        this.setData({
          prepareUrl: prepareUrl
        })



        break;
      case '安全要确认':

        let safecheckUrl = this.data.safecheckUrl

        safecheckUrl.push(e.detail)

        this.setData({
          safecheckUrl: safecheckUrl
        })

        break;
      case '工作要检查':

        let workcheckUrl = this.data.workcheckUrl

        workcheckUrl.push(e.detail)

        this.setData({
          workcheckUrl: workcheckUrl
        })

        break;
      case '交班要清楚':
        let exchangeUrl = this.data.exchangeUrl

        exchangeUrl.push(e.detail)

        this.setData({
          exchangeUrl: exchangeUrl
        })

        break;

      default:
        break;
    }



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})